package cn.com.fssg.platform.base.handler.impl.ryxyxw;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 人员信用行为批量插入数据接口
 */
@Component
public class RyxyxwBatchInsertCtl implements InterfaceHandler {

//    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        String qyryids = jsonObject.get("qyryids").toString();
        List<String> ryidList = Arrays.asList(qyryids.split(","));
        String insertSql = "INSERT INTO bs_gjrysb (id, createid, createname, createtime,bdid,qyryid,rylx) VALUES (:id, :createid, :createname, :createtime,:bdid,:qyryid,:rylx)";
        List<Map<String, Object>> batchParams = new ArrayList<>();
        for (String ryid : ryidList) {
            HashMap<String, Object> params = new HashMap<>();
            params.put("bdid", jsonObject.get("bdid"));
            params.put("qyryid", ryid);
            params.put("rylx", jsonObject.get("rylx"));
            String querySql = "SELECT * FROM bs_gjrysb WHERE qyryid=:qyryid and bdid=:bdid and rylx=:rylx";
            List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(querySql, params);
            // 同一个标段的同一人员职位不能重复
            if (!CollectionUtils.isEmpty(queryForList)) continue;
            // 生成日志记录表
            params.put("id", BizUtil.cur().getSnowflakeIdStr());
            params.put("createid", BizUtil.cur().curUserId());
            params.put("createname", BizUtil.cur().curLoginname());
            params.put("createtime", new Date());
            batchParams.add(params);
        }
        if (!CollectionUtils.isEmpty(batchParams)) {
            jdbcTemplate.batchUpdate(insertSql, batchParams.toArray(new Map[0]));
        }
        return null;
    }
}
